home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_010 / iff / iff.h < prev    next >
C/C++ Source or Header  |  1992-05-06  |  21KB  |  435 lines

  1. #ifndef IFF_H
  2. #define IFF_H
  3. /*----------------------------------------------------------------------*/
  4. /* IFF.H  defs for IFF-85 Interchange Format Files.             10/8/85 */
  5. /*                                                                      */
  6. /* By Jerry Morrison and Steve Shaw, Electronic Arts.                   */
  7. /* This software is in the public domain.                               */
  8. /*----------------------------------------------------------------------*/
  9.  
  10. #include <exec/types.h>
  11. #include <libraries/dos.h>
  12.  
  13. #define NL 0L   /** A ver of NULL So Manx will like it **/
  14.  
  15.  
  16. typedef LONG IFFP;      /* Status code result from an IFF procedure */
  17.       /* LONG, because must be type compatable with ID for GetChunkHdr.*/
  18.         /* Note that the error codes below are not legal IDs.*/
  19. #define IFF_OKAY  0L     /* Keep going...*/
  20. #define END_MARK  -1L    /* As if there was a chunk at end of group.*/
  21. #define IFF_DONE  -2L    /* clientProc returns this when it has READ enough.
  22.                          * It means return thru all levels. File is Okay.*/
  23. #define DOS_ERROR -3L
  24. #define NOT_IFF   -4L   /* not an IFF file.*/
  25. #define NO_FILE   -5L   /* Tried to open file, DOS didn't find it.*/
  26. #define CLIENT_ERROR -6L /* Client made invalid request, for instance, asking
  27.                          * for more bytes than existed in chunk.*/
  28. #define BAD_FORM  -7L    /* A client read proc complains about FORM semantics;
  29.                          * e.g. valid IFF, but missing a required chunk.*/
  30. #define SHORT_CHUNK -8L  /* Client asked to IFFReadBytes more bytes than left
  31.                          * in the chunk. Could be client bug or bad form.*/
  32. #define BAD_IFF   -9L   /* mal-formed IFF file. [TBD] Expand this into a
  33.                          * range of error codes.*/
  34. #define LAST_ERROR (short)BAD_IFF
  35.  
  36. /* This MACRO is used to RETURN immediately when a termination condition is
  37.  * found. This is a pretty weird macro. It requires the caller to declare a
  38.  * local "IFFP iffp" and assign it. This wouldn't work as a subroutine since
  39.  * it returns for it's caller. */
  40. #define CheckIFFP()   { if (iffp != IFF_OKAY) return(iffp); }
  41.  
  42.  
  43. /* ---------- ID -------------------------------------------------------*/
  44.  
  45. typedef LONG ID;        /* An ID is four printable ASCII chars but
  46.                          * stored as a LONG for efficient copy & compare.*/
  47.  
  48. /* Four-character IDentifier builder.*/
  49. #define MakeID(a,b,c,d)  (((long)(a))<<24L | ((long)(b))<<16L | (c)<<8 | (d))
  50.  
  51. /* Standard group IDs.  A chunk with one of these IDs contains a
  52.    SubTypeID followed by zero or more chunks.*/
  53. #define FORM MakeID('F','O','R','M')
  54. #define PROP MakeID('P','R','O','P')
  55. #define LIST MakeID('L','I','S','T')
  56. #define CAT  MakeID('C','A','T',' ')
  57. #define FILLER MakeID(' ',' ',' ',' ')
  58. /* The IDs "FOR1".."FOR9", "LIS1".."LIS9", & "CAT1".."CAT9" are reserved
  59.  * for future standardization.*/
  60.  
  61. /* Pseudo-ID used internally by chunk reader and writer.*/
  62. #define NULL_CHUNK 0L          /* No current chunk.*/
  63.  
  64.  
  65. /* ---------- Chunk ----------------------------------------------------*/
  66.  
  67. /* All chunks start with a type ID and a count of the data bytes that
  68.    follow--the chunk's "logical size" or "data size". If that number is odd,
  69.    a 0 pad byte is written, too. */
  70. typedef struct {
  71.     ID    ckID;
  72.     LONG  ckSize;
  73.     } ChunkHeader;
  74.  
  75. typedef struct {
  76.     ID    ckID;
  77.     LONG  ckSize;
  78.     UBYTE ckData[ 1 /*REALLY: ckSize*/ ];
  79.     } Chunk;
  80.  
  81. /* Pass ckSize = szNotYetKnown to the writer to mean "compute the size".*/
  82. #define szNotYetKnown 0x80000001L
  83.  
  84. /* Need to know whether a value is odd so can word-align.*/
  85. #define IS_ODD(a)   ((a) & 1)
  86.  
  87. /* This macro rounds up to an even number. */
  88. #define WordAlign(size)   ((size+1)&~1)
  89.  
  90. /* ALL CHUNKS MUST BE PADDED TO EVEN NUMBER OF BYTES.
  91.  * ChunkPSize computes the total "physical size" of a padded chunk from
  92.  * its "data size" or "logical size". */
  93. #define ChunkPSize(dataSize)  (WordAlign(dataSize) + (long)sizeof(ChunkHeader))
  94.  
  95. /* The Grouping chunks (LIST, FORM, PROP, & CAT) contain concatenations of
  96.  * chunks after a subtype ID that identifies the content chunks.
  97.  * "FORM type XXXX", "LIST of FORM type XXXX", "PROPerties associated
  98.  * with FORM type XXXX", or "conCATenation of XXXX".*/
  99. typedef struct {
  100.     ID    ckID;
  101.     LONG  ckSize;       /* this ckSize includes "grpSubID".*/
  102.     ID    grpSubID;
  103.     } GroupHeader;
  104.  
  105. typedef struct {
  106.     ID    ckID;
  107.     LONG  ckSize;
  108.     ID    grpSubID;
  109.     UBYTE grpData[ 1 /*REALLY: ckSize-sizeof(grpSubID)*/ ];
  110.     } GroupChunk;
  111.  
  112.  
  113. /* ---------- IFF Reader -----------------------------------------------*/
  114.  
  115. /******** Routines to support a stream-oriented IFF file reader *******
  116.  *
  117.  * These routines handle lots of details like error checking and skipping
  118.  * over padding. They're also careful not to read past any containing context.
  119.  *
  120.  * These routines ASSUME they're the only ones reading from the file.
  121.  * Client should check IFFP error codes. Don't press on after an error!
  122.  * These routines try to have no side effects in the error case, except
  123.  * partial I/O is sometimes unavoidable.
  124.  *
  125.  * All of these routines may return DOS_ERROR. In that case, ask DOS for the
  126.  * specific error code.
  127.  *
  128.  * The overall scheme for the low level chunk reader is to open a "group read
  129.  * context" with OpenRIFF or OpenRGroup, read the chunks with GetChunkHdr
  130.  * (and its kin) and IFFReadBytes, and close the context with CloseRGroup.
  131.  *
  132.  * The overall scheme for reading an IFF file is to use ReadIFF, ReadIList,
  133.  * and ReadICat to scan the file. See those procedures, ClientProc (below),
  134.  * and the skeleton IFF reader. */
  135.  
  136. /* Client passes ptrs to procedures of this type to ReadIFF which call them
  137.  * back to handle LISTs, FORMs, CATs, and PROPs.
  138.  *
  139.  * Use the GroupContext ptr when calling reader routines like GetChunkHdr.
  140.  * Look inside the GroupContext ptr for your ClientFrame ptr. You'll
  141.  * want to type cast it into a ptr to your containing struct to get your
  142.  * private contextual data (stacked property settings). See below. */
  143. typedef IFFP ClientProc(/* struct _GroupContext * */);
  144.  
  145. /* Client's context for reading an IFF file or a group.
  146.  * Client should actually make this the first component of a larger struct
  147.  * (it's personal stack "frame") that has a field to store each "interesting"
  148.  * property encountered.
  149.  * Either initialize each such field to a global default or keep a boolean
  150.  * indicating if you've read a property chunk into that field.
  151.  * Your getList and getForm procs should allocate a new "frame" and copy the
  152.  * parent frame's contents. The getProp procedure should store into the frame
  153.  * allocated by getList for the containing LIST. */
  154. typedef struct _ClientFrame {
  155.     ClientProc *getList, *getProp, *getForm, *getCat;
  156.     /* client's own data follows; place to stack property settings */
  157.     } ClientFrame;
  158.  
  159. /* Our context for reading a group chunk. */
  160. typedef struct _GroupContext {
  161.     struct _GroupContext *parent; /* Containing group; NULL => whole file. */
  162.     ClientFrame *clientFrame;     /* Reader data & client's context state. */
  163.     BPTR file;          /* Byte-stream file handle. */
  164.     LONG position;      /* The context's logical file position. */
  165.     LONG bound;         /* File-absolute context bound
  166.                          * or szNotYetKnown (writer only). */
  167.     ChunkHeader ckHdr;  /* Current chunk header. ckHdr.ckSize = szNotYetKnown
  168.                          * means we need to go back and set the size (writer onl
  169. y).
  170.                          * See also Pseudo-IDs, above. */
  171.     ID subtype;         /* Group's subtype ID when reading. */
  172.     LONG bytesSoFar;    /* # bytes read/written of current chunk's data. */
  173.     } GroupContext;
  174.  
  175. /* Computes the number of bytes not yet read from the current chunk, given
  176.  * a group read context gc. */
  177. #define ChunkMoreBytes(gc)  ((gc)->ckHdr.ckSize - (gc)->bytesSoFar)
  178.  
  179.  
  180. /***** Low Level IFF Chunk Reader *****/
  181.  
  182. /* Given an open file, open a read context spanning the whole file.
  183.  * This is normally only called by ReadIFF.
  184.  * This sets new->clientFrame = clientFrame.
  185.  * ASSUME context allocated by caller but not initialized.
  186.  * ASSUME caller doesn't deallocate the context before calling CloseRGroup.
  187.  * NOT_IFF ERROR if the file is too short for even a chunk header.*/
  188. extern IFFP OpenRIFF(/* BPTR, GroupContext *, ClientFrame * */);
  189.                      /* file, new,            clientFrame  */
  190.  
  191. /* Open the remainder of the current chunk as a group read context.
  192.  * This will be called just after the group's subtype ID has been read
  193.  * (automatically by GetChunkHdr for LIST, FORM, PROP, and CAT) so the
  194.  * remainder is a sequence of chunks.
  195.  * This sets new->clientFrame = parent->clientFrame. The caller should repoint
  196.  * it at a new clientFrame if opening a LIST context so it'll have a "stack
  197.  * frame" to store PROPs for the LIST. (It's usually convenient to also
  198.  * allocate a new Frame when you encounter FORM of the right type.)
  199.  *
  200.  * ASSUME new context allocated by caller but not initialized.
  201.  * ASSUME caller doesn't deallocate the context or access the parent context
  202.  * before calling CloseRGroup.
  203.  * BAD_IFF ERROR if context end is odd or extends past parent. */
  204. extern IFFP OpenRGroup(/* GroupContext *, GroupContext * */);
  205.                        /* parent,         new  */
  206.  
  207. /* Close a group read context, updating its parent context.
  208.  * After calling this, the old context may be deallocated and the parent
  209.  * context can be accessed again. It's okay to call this particular procedure
  210.  * after an error has occurred reading the group.
  211.  * This always returns IFF_OKAY. */
  212. extern IFFP CloseRGroup(/* GroupContext * */);
  213.                         /* old  */
  214.  
  215. /* Skip any remaining bytes of the previous chunk and any padding, then
  216.  * read the next chunk header into context.ckHdr.
  217.  * If the ckID is LIST, FORM, CAT, or PROP, this automatically reads the
  218.  * subtype ID into context->subtype.
  219.  * Caller should dispatch on ckID (and subtype) to an appropriate handler.
  220.  *
  221.  * RETURNS context.ckHdr.ckID (the ID of the new chunk header); END_MARK
  222.  * if there are no more chunks in this context; or NOT_IFF if the top level
  223.  * file chunk isn't a FORM, LIST, or CAT; or BAD_IFF if malformed chunk, e.g.
  224.  * ckSize is negative or too big for containing context, ckID isn't positive,
  225.  * or we hit end-of-file.
  226.  *
  227.  * See also GetFChunkHdr, GetF1ChunkHdr, and GetPChunkHdr, below.*/
  228. extern ID       GetChunkHdr(/* GroupContext * */);
  229.   /*  context.ckHdr.ckID       context  */
  230.  
  231. /* Read nBytes number of data bytes of current chunk. (Use OpenGroup, etc.
  232.  * instead to read the contents of a group chunk.) You can call this several
  233.  * times to read the data piecemeal.
  234.  * CLIENT_ERROR if nBytes < 0. SHORT_CHUNK if nBytes > ChunkMoreBytes(context)
  235.  * which could be due to a client bug or a chunk that's shorter than it
  236.  * ought to be (bad form). (on either CLIENT_ERROR or SHORT_CHUNK,
  237.  * IFFReadBytes won't read any bytes.) */
  238. extern IFFP IFFReadBytes(/* GroupContext *, BYTE *, LONG */);
  239.                          /* context,        buffer, nBytes  */
  240.  
  241.  
  242. /***** IFF File Reader *****/
  243.  
  244. /* This is a noop ClientProc that you can use for a getList, getForm, getProp,
  245.  * or getCat procedure that just skips the group. A simple reader might just
  246.  * implement getForm, store &ReadICat in the getCat field of clientFrame, and
  247.  * use &SkipGroup for the getList and getProp procs.*/
  248. extern IFFP SkipGroup(/* GroupContext * */);
  249.  
  250. /* IFF file reader.
  251.  * Given an open file, allocate a group context and use it to read the FORM,
  252.  * LIST, or CAT and it's contents. The idea is to parse the file's contents,
  253.  * and for each FORM, LIST, CAT, or PROP encountered, call the getForm,
  254.  * getList, getCat, or getProp procedure in clientFrame, passing the
  255.  * GroupContext ptr.
  256.  * This is achieved with the aid of ReadIList (which your getList should
  257.  * call) and ReadICat (which your getCat should call, if you don't just use
  258.  * &ReadICat for your getCat). If you want to handle FORMs, LISTs, and CATs
  259.  * nested within FORMs, the getForm procedure must dispatch to getForm,
  260.  * getList, and getCat (it can use GetF1ChunkHdr to make this easy).
  261.  *
  262.  * Normal return is IFF_OKAY (if whole file scanned) or IFF_DONE (if a client
  263.  * proc said "done" first).
  264.  * See the skeletal getList, getForm, getCat, and getProp procedures. */
  265. extern IFFP ReadIFF(/* BPTR, ClientFrame * */);
  266.                     /* file, clientFrame  */
  267.  
  268. /* IFF LIST reader.
  269.  * Your "getList" procedure should allocate a ClientFrame, copy the parent's
  270.  * ClientFrame, and then call this procedure to do all the work.
  271.  *
  272.  * Normal return is IFF_OKAY (if whole LIST scanned) or IFF_DONE (if a client
  273.  * proc said "done" first).
  274.  * BAD_IFF ERROR if a PROP appears after a non-PROP. */
  275. extern IFFP ReadIList(/* GroupContext *, ClientFrame * */);
  276.                       /* parent,         clientFrame  */
  277.  
  278. /* IFF CAT reader.
  279.  * Most clients can simply use this to read their CATs. If you must do extra
  280.  * setup work, put a ptr to your getCat procedure in the clientFrame, and
  281.  * have that procedure call ReadICat to do the detail work.
  282.  *
  283.  * Normal return is IFF_OKAY (if whole CAT scanned) or IFF_DONE (if a client
  284.  * proc said "done" first).
  285.  * BAD_IFF ERROR if a PROP appears in the CAT. */
  286. extern IFFP ReadICat(/* GroupContext * */);
  287.                      /* parent  */
  288.  
  289. /* Call GetFChunkHdr instead of GetChunkHdr to read each chunk inside a FORM.
  290.  * It just calls GetChunkHdr and returns BAD_IFF if it gets a PROP chunk. */
  291. extern ID       GetFChunkHdr(/* GroupContext * */);
  292.   /*  context.ckHdr.ckID        context  */
  293.  
  294. /* GetF1ChunkHdr is like GetFChunkHdr, but it automatically dispatches to the
  295.  * getForm, getList, and getCat procedure (and returns the result) if it
  296.  * encounters a FORM, LIST, or CAT. */
  297. extern ID       GetF1ChunkHdr(/* GroupContext * */);
  298.   /*  context.ckHdr.ckID         context  */
  299.  
  300. /* Call GetPChunkHdr instead of GetChunkHdr to read each chunk inside a PROP.
  301.  * It just calls GetChunkHdr and returns BAD_IFF if it gets a group chunk. */
  302. extern ID       GetPChunkHdr(/* GroupContext * */);
  303.   /*  context.ckHdr.ckID        context  */
  304.  
  305.  
  306. /* ---------- IFF Writer -----------------------------------------------*/
  307.  
  308. /******* Routines to support a stream-oriented IFF file writer *******
  309.  *
  310.  * These routines will random access back to set a chunk size value when the
  311.  * caller doesn't know it ahead of time. They'll also do things automatically
  312.  * like padding and error checking.
  313.  *
  314.  * These routines ASSUME they're the only ones writing to the file.
  315.  * Client should check IFFP error codes. Don't press on after an error!
  316.  * These routines try to have no side effects in the error case, except that
  317.  * partial I/O is sometimes unavoidable.
  318.  *
  319.  * All of these routines may return DOS_ERROR. In that case, ask DOS for the
  320.  * specific error code.
  321.  *
  322.  * The overall scheme is to open an output GroupContext via OpenWIFF or
  323.  * OpenWGroup, call either PutCk or {PutCkHdr {IFFWriteBytes}* PutCkEnd} for
  324.  * each chunk, then use CloseWGroup to close the GroupContext.
  325.  *
  326.  * To write a group (LIST, FORM, PROP, or CAT), call StartWGroup, write out
  327.  * its chunks, then call EndWGroup. StartWGroup automatically writes the
  328.  * group header and opens a nested context for writing the contents.
  329.  * EndWGroup closes the nested context and completes the group chunk. */
  330.  
  331.  
  332. /* Given a file open for output, open a write context.
  333.  * The "limit" arg imposes a fence or upper limit on the logical file
  334.  * position for writing data in this context. Pass in szNotYetKnown to be
  335.  * bounded only by disk capacity.
  336.  * ASSUME new context structure allocated by caller but not initialized.
  337.  * ASSUME caller doesn't deallocate the context before calling CloseWGroup.
  338.  * The caller is only allowed to write out one FORM, LIST, or CAT in this top
  339.  * level context (see StartWGroup and PutCkHdr).
  340.  * CLIENT_ERROR if limit is odd.*/
  341. extern IFFP OpenWIFF(/* BPTR, GroupContext *, LONG */);
  342.                      /* file, new,            limit {file position}  */
  343.  
  344. /* Start writing a group (presumably LIST, FORM, PROP, or CAT), opening a
  345.  * nested context. The groupSize includes all nested chunks + the subtype ID.
  346.  *
  347.  * The subtype of a LIST or CAT is a hint at the contents' FORM type(s). Pass
  348.  * in FILLER if it's a mixture of different kinds.
  349.  *
  350.  * This writes the chunk header via PutCkHdr, writes the subtype ID via
  351.  * IFFWriteBytes, and calls OpenWGroup. The caller may then write the nested
  352.  * chunks and finish by calling EndWGroup.
  353.  * The OpenWGroup call sets new->clientFrame = parent->clientFrame.
  354.  *
  355.  * ASSUME new context structure allocated by caller but not initialized.
  356.  * ASSUME caller doesn't deallocate the context or access the parent context
  357.  * before calling CloseWGroup.
  358.  * ERROR conditions: See PutCkHdr, IFFWriteBytes, OpenWGroup. */
  359. extern IFFP StartWGroup(/* GroupContext *, ID, LONG, ID, GroupContext * */);
  360.                         /* parent, groupType, groupSize, subtype, new  */
  361.  
  362. /* End a group started by StartWGroup.
  363.  * This just calls CloseWGroup and PutCkEnd.
  364.  * ERROR conditions: See CloseWGroup and PutCkEnd. */
  365. extern IFFP EndWGroup(/* GroupContext * */);
  366.                       /* old  */
  367.  
  368. /* Open the remainder of the current chunk as a group write context.
  369.  * This is normally only called by StartWGroup.
  370.  *
  371.  * Any fixed limit to this group chunk or a containing context will impose
  372.  * a limit on the new context.
  373.  * This will be called just after the group's subtype ID has been written
  374.  * so the remaining contents will be a sequence of chunks.
  375.  * This sets new->clientFrame = parent->clientFrame.
  376.  * ASSUME new context structure allocated by caller but not initialized.
  377.  * ASSUME caller doesn't deallocate the context or access the parent context
  378.  * before calling CloseWGroup.
  379.  * CLIENT_ERROR if context end is odd or PutCkHdr wasn't called first. */
  380. extern IFFP OpenWGroup(/* GroupContext *, GroupContext * */);
  381.                        /* parent,         new  */
  382.  
  383. /* Close a write context and update its parent context.
  384.  * This is normally only called by EndWGroup.
  385.  *
  386.  * If this is a top level context (created by OpenWIFF) we'll set the file's
  387.  * EOF (end of file) but won't close the file.
  388.  * After calling this, the old context may be deallocated and the parent
  389.  * context can be accessed again.
  390.  *
  391.  * Amiga DOS Note: There's no call to set the EOF. We just position to the
  392.  * desired end and return. Caller must Close file at that position.
  393.  * CLIENT_ERROR if PutCkEnd wasn't called first. */
  394. extern IFFP CloseWGroup(/* GroupContext * */);
  395.                         /* old  */
  396.  
  397. /* Write a whole chunk to a GroupContext. This writes a chunk header, ckSize
  398.  * data bytes, and (if needed) a pad byte. It also updates the GroupContext.
  399.  * CLIENT_ERROR if ckSize == szNotYetKnown. See also PutCkHdr errors. */
  400. extern IFFP PutCk(/* GroupContext *, ID,   LONG,   BYTE * */);
  401.                   /* context,        ckID, ckSize, *data  */
  402.  
  403. /* Write just a chunk header. Follow this will any number of calls to
  404.  * IFFWriteBytes and finish with PutCkEnd.
  405.  * If you don't yet know how big the chunk is, pass in ckSize = szNotYetKnown,
  406.  * then PutCkEnd will set the ckSize for you later.
  407.  * Otherwise, IFFWriteBytes and PutCkEnd will ensure that the specified
  408.  * number of bytes get written.
  409.  * CLIENT_ERROR if the chunk would overflow the GroupContext's bound, if
  410.  * PutCkHdr was previously called without a matching PutCkEnd, if ckSize < 0
  411.  * (except szNotYetKnown), if you're trying to write something other
  412.  * than one FORM, LIST, or CAT in a top level (file level) context, or
  413.  * if ckID <= 0 (these illegal ID values are used for error codes). */
  414. extern IFFP PutCkHdr(/* GroupContext *, ID,   LONG */);
  415.                      /* context,        ckID, ckSize  */
  416.  
  417. /* Write nBytes number of data bytes for the current chunk and update
  418.  * GroupContext.
  419.  * CLIENT_ERROR if this would overflow the GroupContext's limit or the
  420.  * current chunk's ckSize, or if PutCkHdr wasn't called first, or if
  421.  * nBytes < 0. */
  422. extern IFFP IFFWriteBytes(/* GroupContext *, BYTE *, LONG */);
  423.                           /* context,        *data,  nBytes  */
  424.  
  425. /* Complete the current chunk, write a pad byte if needed, and update
  426.  * GroupContext.
  427.  * If current chunk's ckSize = szNotYetKnown, this goes back and sets the
  428.  * ckSize in the file.
  429.  * CLIENT_ERROR if PutCkHdr wasn't called first, or if client hasn't
  430.  * written 'ckSize' number of bytes with IFFWriteBytes. */
  431. extern IFFP PutCkEnd(/* GroupContext * */);
  432.                      /* context  */
  433.  
  434. #endif
  435.